package com.ysd.lis.service.lab.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysParamSearchOutputDto;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.*;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.common.RedisSysParamHelper;
import com.ysd.lis.service.lab.LabMaininfoService;
import com.ysd.lis.util.lab.CalcItemFormulaReplcae;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class LabRptresultCalcServiceImpl extends ServiceImpl<LabRptresultMapper, LabRptresult> {
    @Autowired
    LabMaininfoService labMaininfoService;

    @Autowired
    LabMaininfoMapper labMaininfoMapper;

    @Autowired
    LabRptresultMapper labRptresultMapper;

    @Autowired
    LabItemDictMapper labItemDictMapper;

    @Autowired
    LabItemDictReferMapper labItemDictReferMapper;

    @Autowired
    LabItemDictCriticalMapper labItemDictCriticalMapper;

    @Autowired
    LabTransResultMapper labTransResultMapper;

    @Autowired
    LabTextResultMapper labTextResultMapper;

    @Autowired
    LabFeeitemVsItemDictMapper labFeeitemVsItemDictMapper;

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Autowired
    LabChannelMapper labChannelMapper;

    @Autowired
    RedisSysParamHelper redisSysParamHelper;

    @Autowired
    LabItemDictSpecialRulesMapper labItemDictSpecialRulesMapper;

    /*获取出项目字典*/
    public List<LabItemDict> getItemDictList(String rptGroup) {
        LambdaQueryWrapper<LabItemDict> _labitemdictQuery = new LambdaQueryWrapper<>();
        _labitemdictQuery.eq(LabItemDict::getRptGroup, rptGroup);
        _labitemdictQuery.eq(LabItemDict::getDelFlag, 0);
        _labitemdictQuery.eq(LabItemDict::getEnable, 1);
        _labitemdictQuery.orderByAsc(LabItemDict::getSeq);
        List<LabItemDict> itemList = labItemDictMapper.selectList(_labitemdictQuery);
        return CollectionUtils.isNotEmpty(itemList) ? itemList : null;
    }

    /*文字结果处理*/
    public void CalcTextResultV1(LabMaininfo mainInfo, List<LabRptresult> rptresultlist, List<LabItemDict> itemList) {
        MPJLambdaWrapper<LabTextResult> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabTextResult::getDelFlag, "0");
        queryWrapper.eq(LabTextResult::getRptGroup, mainInfo.getRptGroup());
        List<String> itemNos = rptresultlist.stream().map(LabRptresult::getItemNo).collect(Collectors.toList());

        if (!itemNos.isEmpty()) {
            queryWrapper.in(LabTextResult::getItemNo, itemNos);
        } else {
            queryWrapper.eq(LabTextResult::getItemDictId, "-123!@#");
        }
        queryWrapper.orderByAsc(LabTextResult::getSeq);
        List<LabTextResult> LabTextResults = labTextResultMapper.selectList(queryWrapper);

        /*查询报告项目备用*/
        LambdaQueryWrapper<LabItemDict> _labitemdictQuery = new LambdaQueryWrapper<>();
        _labitemdictQuery.eq(LabItemDict::getRptGroup, mainInfo.getRptGroup());
        _labitemdictQuery.eq(LabItemDict::getDelFlag, 0);
        _labitemdictQuery.eq(LabItemDict::getEnable, 1);
        _labitemdictQuery.orderByAsc(LabItemDict::getSeq);

        LinkedList<LabRptresult> list = new LinkedList();
        list.addAll(rptresultlist);

        try {
            for (LabRptresult item : list) {
                try {
                    if (StringUtils.isEmpty(item.getTestResult())) {
                        continue;
                    }
                    //报告项目
                    List<LabItemDict> itmLst = itemList.stream().filter(p -> p.getItemNo().equals(item.getItemNo()) && p.getRptGroup().equals(item.getRptGroup())).collect(Collectors.toList());
                    LabItemDict itemDict = null;
                    if (!itmLst.isEmpty()) {
                        itemDict = itmLst.get(0);
                    }
                    if (BeanUtil.isEmpty(itemDict)) {
                        continue;
                    }
                    String itmDictIdStr = itemDict.getId();
                    List<LabTextResult> _lst = LabTextResults.stream().filter(p -> p.getRptGroup().equals(item.getRptGroup()) && p.getItemNo().equals(item.getItemNo())).collect(Collectors.toList());
                    String r = item.getTestResult();
                    String rf = "M";
                    String wjz = "";
                    Integer isWjz = 0;

                    for (LabTextResult _itmRefer : _lst) {
                        if (StringUtils.isEmpty(_itmRefer.getResult())) {
                            continue;
                        }
                        try {
                            //transtype 判断方式
                            //1相等
                            //2包含(%常用值%)
                            //3包含(常用值%)
                            //4包含(%常用值)
                            if (BeanUtil.isNotEmpty(_itmRefer.getTranstype()) && 1 == _itmRefer.getTranstype().intValue()) {
                                if (!StringUtils.isEmpty(_itmRefer.getResult()) && _itmRefer.getResult().equals(item.getTestResult())) {
                                    rf = _itmRefer.getResultSign();
                                    //是否危急值
                                    if (BeanUtil.isNotEmpty(_itmRefer.getIsCritical()) && _itmRefer.getIsCritical().intValue() == 1) {
                                        isWjz = 1;
                                        wjz = _itmRefer.getDisplayText();
                                    }
                                }
                            }
                            if (BeanUtil.isNotEmpty(_itmRefer.getTranstype()) && 2 == _itmRefer.getTranstype().intValue()) {
                                if (!StringUtils.isEmpty(_itmRefer.getResult()) && item.getTestResult().contains((_itmRefer.getResult()))) {
                                    rf = _itmRefer.getResultSign();
                                    //是否危急值
                                    if (BeanUtil.isNotEmpty(_itmRefer.getIsCritical()) && _itmRefer.getIsCritical().intValue() == 1) {
                                        isWjz = 1;
                                        wjz = _itmRefer.getDisplayText();
                                    }
                                }
                            }
                            if (BeanUtil.isNotEmpty(_itmRefer.getTranstype()) && 3 == _itmRefer.getTranstype().intValue()) {
                                if (!StringUtils.isEmpty(_itmRefer.getResult()) && item.getTestResult().startsWith((_itmRefer.getResult()))) {
                                    rf = _itmRefer.getResultSign();
                                    //是否危急值
                                    if (BeanUtil.isNotEmpty(_itmRefer.getIsCritical()) && _itmRefer.getIsCritical().intValue() == 1) {
                                        isWjz = 1;
                                        wjz = _itmRefer.getDisplayText();
                                    }
                                }
                            }
                            if (BeanUtil.isNotEmpty(_itmRefer.getTranstype()) && 4 == _itmRefer.getTranstype().intValue()) {
                                if (!StringUtils.isEmpty(_itmRefer.getResult()) && item.getTestResult().endsWith((_itmRefer.getResult()))) {
                                    rf = _itmRefer.getResultSign();
                                    //是否危急值
                                    if (BeanUtil.isNotEmpty(_itmRefer.getIsCritical()) && _itmRefer.getIsCritical().intValue() == 1) {
                                        isWjz = 1;
                                        wjz = _itmRefer.getDisplayText();
                                    }
                                }
                            }
                            item.setIsAlarm(isWjz);
                            /*文字结果危急值 没有低危 高危之分 显示颜色采用系统默认*/
                            item.setAlarmType("");
                            item.setCatchColor("");
                            item.setColorFor("0");
                            item.setAlarmText(wjz);
                            item.setResultFlag(rf);

                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            rptresultlist = list;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*结果二次处理*/
    public void CalcTransResultV1(LabMaininfo mainInfo, List<LabRptresult> rptresultlist, List<LabItemDict> itemList) {
        MPJLambdaWrapper<LabTransResult> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabTransResult::getDelFlag, "0");
        queryWrapper.eq(LabTransResult::getRptGroup, mainInfo.getRptGroup());
        List<String> itemNos = rptresultlist.stream().map(LabRptresult::getItemNo).collect(Collectors.toList());

        if (!itemNos.isEmpty()) {
            queryWrapper.in(LabTransResult::getItemNo, itemNos);
        } else {
            queryWrapper.eq(LabTransResult::getItemNo, "-123!@#");
        }
        queryWrapper.orderByAsc(LabTransResult::getPriority);
        List<LabTransResult> LabTransResults = labTransResultMapper.selectList(queryWrapper);

        try {
            for (LabRptresult item : rptresultlist) {
                if (StringUtils.isEmpty(item.getTestResult())) {
                    continue;
                }
                //报告项目
                List<LabItemDict> itmLst = itemList.stream().filter(p -> p.getItemNo().equals(item.getItemNo()) && p.getRptGroup().equals(item.getRptGroup())).collect(Collectors.toList());
                LabItemDict itemDict = null;
                if (!itmLst.isEmpty()) {
                    itemDict = itmLst.get(0);
                }

                if (BeanUtil.isEmpty(itemDict) || StringUtils.isEmpty(itemDict.getItemNo())) {
                    continue;
                }
                String itmDictCodeStr = itemDict.getItemNo();

                List<LabTransResult> _lst = LabTransResults.stream().filter(p -> p.getItemNo().equals(itmDictCodeStr) && p.getRptGroup().equals(item.getRptGroup())).collect(Collectors.toList());
                String r = item.getTestResult();
                String rf = "";
                for (LabTransResult _itmRefer : _lst) {
                    if (StringUtils.isEmpty(_itmRefer.getConparam1()) && StringUtils.isEmpty(_itmRefer.getConparam2())) {
                        continue;
                    }
                    try {
                        //结果等于=
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("1") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (r.equals(_itmRefer.getConparam1())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (r.equals(_itmRefer.getConparam2())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }
                        }
                        //结果等于>
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("2") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam1())) > 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam2())) > 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }

                        }
                        //结果等于<
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("3") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam1())) < 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam2())) < 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }

                        }
                        //结果等于>=
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("4") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam1())) >= 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                        //item.setResultFlag(_itmRefer.getResultflag());
                                    }
                                }
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam2())) >= 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }

                        }
                        //结果等于<=
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("5") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam1())) <= 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (new BigDecimal(r).compareTo(new BigDecimal(_itmRefer.getConparam2())) <= 0) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }

                        }
                        //结果包含
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("6") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (r.contains(_itmRefer.getConparam1())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (r.contains(_itmRefer.getConparam2())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }

                        }
                        //结果开头=
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("7") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (r.startsWith(_itmRefer.getConparam1())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (r.startsWith(_itmRefer.getConparam2())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }

                        }
                        //结果结尾=
                        if (!StringUtils.isEmpty(_itmRefer.getCondition()) && _itmRefer.getCondition().equals("8") && !StringUtils.isEmpty(item.getTestResult())) {
                            try {
                                if (r.endsWith(_itmRefer.getConparam1())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam1();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam1(), _itmRefer.getTransparam1());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                                if (r.endsWith(_itmRefer.getConparam2())) {
                                    String res = r;
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("1")) {
                                        res = _itmRefer.getTransparam2();
                                    }
                                    if (!StringUtils.isEmpty(_itmRefer.getTranstype()) && _itmRefer.getTranstype().equals("2")) {
                                        res = r.replace(_itmRefer.getConparam2(), _itmRefer.getTransparam2());
                                    }
                                    item.setTestResult(res);
                                    if (!StringUtils.isEmpty(_itmRefer.getResultflag())) {
                                        rf = _itmRefer.getResultflag();
                                    }
                                }
                            } catch (Exception e) {
                                //TODO
                            }
                        }
                    } catch (Exception ex) {
                        //todo
                    }
                }
                if (!StringUtils.isEmpty(rf)) {
                    item.setResultFlag(rf);
                }
            }
        } catch (Exception e) {
            //TODO
        }
    }

    /*计算项目处理*/
    public Result CalcFormulaItemResultV1(LabMaininfo mainInfo, List<LabRptresult> rptresultlist, List<LabItemDict> itemList) {
        try {
            for (LabItemDict item : itemList) {
                String formula = item.getCalcItemFormula();
                if (StringUtils.isEmpty(formula)) {
                    continue;
                }
                formula = new CalcItemFormulaReplcae().ReplaceCondition(formula, mainInfo, null);/*先替换掉申请中的信息*/
                //----------------------------------------------公式计算
                Pattern pattern = Pattern.compile("(?i)(?<=\\[)[^\\[]*(?=\\])");
                Matcher matcher = pattern.matcher(formula);

                List<Result> currentResultList = new ArrayList<>();
                // Add relevant Result objects to currentResultList
                boolean isBreak = false;
                while (matcher.find()) {
                    String matchColMatchValue = matcher.group();

                    String matchResult = "";
                    for (LabRptresult result : rptresultlist) {
                        if (result.getItemNo().equals(matchColMatchValue)) {
                            matchResult = result.getTestResult();
                            break;
                        }
                    }

                    if (StringUtils.isEmpty(matchResult)) {
                        isBreak = true;
                        break;
                    }
                    try {
                        matchResult = new BigDecimal(matchResult).setScale(2, RoundingMode.HALF_UP).toString();
                    } catch (Exception e) {

                    }

                    formula = formula.replace("[" + matchColMatchValue + "]", matchResult);
                    //----------------------------------------------
                }
                if (!isBreak) {

                    String ResValue = "";
                    try {
                        String sql = String.format("select " + formula + " as RESULT from dual ");
                        try {
                            List<Map<String, Object>> calcResult = labRptresultMapper.getTableDataBySql(sql);
                            // 遍历 List 中的每个 Map
                            for (Map<String, Object> map : calcResult) {
                                System.out.println(map);
                                ResValue = map.get("RESULT").toString();
                            }

                        } catch (Exception e) {
                            ResValue = "0.00";
                        }
                    } catch (Exception exception) {
                        //TODO
                        exception.printStackTrace();
                    }

                    //保留小数位数
                    String DecPos = item.getDecPos();
                    Integer _decPos = 0;
                    switch (DecPos) {
                        case "1":
                            _decPos = 1;
                            break;
                        case "2":
                            _decPos = 2;
                            break;
                        case "3":
                            _decPos = 3;
                            break;
                        case "4":
                            _decPos = 4;
                            break;
                        case "5":
                            _decPos = 5;
                            break;
                        case "6":
                            _decPos = 6;
                            break;
                    }
                    //检查结果中是否已经存在该计算项目
                    boolean isExist = false;
                    for (LabRptresult _item : rptresultlist) {
                        if (_item.getItemNo().equals(item.getItemNo())) {
                            _item.setTestResult(ResValue);
                            _item.setIsResult(1);
                            _item.setDisplayOrder(item.getSeq().toString());
                            _item.setRef(item.getRef());
                            _item.setItemDictId(item.getId());
                            //保留小数位数
                            if (item.getItemKind().equals("1")) {
                                BigDecimal decimalNumber = new BigDecimal(ResValue);
                                decimalNumber = decimalNumber.setScale(_decPos, BigDecimal.ROUND_HALF_UP); // 设置小数位数并进行四舍五入
                                _item.setTestResult(decimalNumber.toString());
                            }
                            isExist = true;
                            break;
                        }
                    }
                    //如果不存在，则新增该计算项目
                    if (!isExist) {
                        LabRptresult newCalcItemResult = new LabRptresult();
                        BeanUtil.copyProperties(rptresultlist.get(0), newCalcItemResult, "id", "isAlarm", "alarmText", "alarmType", "catchColor", "colorFor", "alarmText", "rawData", "sco", "ct", "od", "cutoff", "resultFlag", "resultTimeStr", "ref", "resultDesc", "remark1", "remark2", "remark3", "remark4", "itemDictId", "feeCode", "feeName", "intResultId", "instrNa", "instrId", "adjustResult");
                        newCalcItemResult.setTestResult(ResValue);
                        newCalcItemResult.setIsResult(1);
                        //保留小数位数
                        if (item.getItemKind().equals("1")) {
                            BigDecimal decimalNumber = new BigDecimal(ResValue);
                            decimalNumber = decimalNumber.setScale(_decPos, BigDecimal.ROUND_HALF_UP); // 设置小数位数并进行四舍五入
                            newCalcItemResult.setTestResult(decimalNumber.toString());
                        }

                        /*处理结果中的申请项目,因为扫码上机时能带出对照的项目  即使是  计算项目*/

                        newCalcItemResult.setDisplayOrder(item.getSeq().toString());
                        newCalcItemResult.setItemNo(item.getItemNo());
                        newCalcItemResult.setItemNa(item.getItemNa());
                        newCalcItemResult.setRef(item.getRef());
                        newCalcItemResult.setItemDictId(item.getId());
                        rptresultlist.add(newCalcItemResult);
                    }
                }

            }
            CalcItemDictRefersV1(mainInfo, rptresultlist, itemList);
            return Result.succ("计算成功");
        } catch (Exception exception) {
            //TODO
            exception.printStackTrace();
        }

        return Result.fail("计算失败");
    }

    /*参考范围*/
    public void CalcItemDictRefersV1(LabMaininfo mainInfo, List<LabRptresult> rptresultlist, List<LabItemDict> itemList) {
        MPJLambdaWrapper<LabItemDictRefer> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabItemDictRefer::getDelFlag, "0");
        queryWrapper.eq(LabItemDictRefer::getIsEnable, "1");
        queryWrapper.eq(LabItemDictRefer::getRptGroup, mainInfo.getRptGroup());
        List<String> itemNos = rptresultlist.stream().map(LabRptresult::getItemNo).collect(Collectors.toList());

        if (!itemNos.isEmpty()) {
            queryWrapper.in(LabItemDictRefer::getItemNo, itemNos);
        } else {
            queryWrapper.eq(LabItemDictRefer::getOrgId, "-123!@#");
        }
        queryWrapper.orderByAsc(LabItemDictRefer::getSeq);
        List<LabItemDictRefer> LabItemDictRefers = labItemDictReferMapper.selectList(queryWrapper);

        /*开始循环计算每一项的参考范围*/
        try {
            try {
                ExecutorService executorService = Executors.newFixedThreadPool(rptresultlist.size()); // 创建一个线程池
                CountDownLatch countDownLatch = new CountDownLatch(rptresultlist.size());

                for (LabRptresult item : rptresultlist) {
                    Future<?> future = executorService.submit(() -> {
                        //报告项目
                        try {
                            List<LabItemDict> itmLst = itemList.stream().filter(p -> p.getItemNo().equals(item.getItemNo()) && p.getRptGroup().equals(item.getRptGroup())).collect(Collectors.toList());
                            LabItemDict itemDict = null;
                            if (!itmLst.isEmpty()) {
                                itemDict = itmLst.get(0);
                            }

                            if (BeanUtil.isEmpty(itemDict)) {
                                return;
                            }

                            //先带出参考范围
                            item.setRef(StringUtils.isEmpty(itemDict.getRef()) ? "" : itemDict.getRef());
                            try {
                                if (!StringUtils.isEmpty(itemDict.getRef())) {
                                    /*解决青岛优抚  结果+3  变为3的BUG */
                                    if (ToolsUtils.isNotEmpty(itemDict.getItemKind()) && "1".equals(itemDict.getItemKind())) {
                                        BigDecimal decimalNumber = new BigDecimal(item.getTestResult());
                                        item.setResultFlag("M");
                                    }
                                }
                            } catch (Exception e) {
                                System.out.println("结果转换bigdecimal异常！");
                            }
                            //保留小数位数
                            String DecPos = itemDict.getDecPos();
                            Integer _decPos = 0;
                            switch (DecPos) {
                                case "1":
                                    _decPos = 1;
                                    break;
                                case "2":
                                    _decPos = 2;
                                    break;
                                case "3":
                                    _decPos = 3;
                                    break;
                                case "4":
                                    _decPos = 4;
                                    break;
                                case "5":
                                    _decPos = 5;
                                    break;
                                case "6":
                                    _decPos = 6;
                                    break;
                            }
                            boolean isDetailRef = false;
                            String refStr = itemDict.getRef();
                            try {
                                BigDecimal decimalNumber1 = new BigDecimal(item.getTestResult());
                                decimalNumber1 = decimalNumber1.setScale(_decPos, BigDecimal.ROUND_HALF_UP); // 设置小数位数并进行四舍五入
                                /*解决青岛优抚  结果+3  变为3的BUG */
                                if (ToolsUtils.isNotEmpty(itemDict.getItemKind()) && "1".equals(itemDict.getItemKind())) {
                                    item.setTestResult(decimalNumber1.toString());
                                }
                            } catch (Exception e) {
                                System.out.println("结果转换bigdecimal异常！");
                            }
                            List<LabItemDictRefer> _lst = LabItemDictRefers.stream().filter(p -> p.getRptGroup().equals(item.getRptGroup()) && p.getItemNo().equals(item.getItemNo())).collect(Collectors.toList());
                            for (LabItemDictRefer _itmRefer : _lst) {
                                boolean isFactorTrue = false;/*是否已有满足的条件*/
                                //没有条件
                                if (StringUtils.isEmpty(_itmRefer.getFactor())) {
                                    if (itemDict.getItemKind().equals("1")) {
                                        //检测结果
                                        if (!StringUtils.isEmpty(item.getTestResult())) {
                                            try {
                                                BigDecimal decimalNumber = new BigDecimal(item.getTestResult());
                                                decimalNumber = decimalNumber.setScale(_decPos, BigDecimal.ROUND_HALF_UP); // 设置小数位数并进行四舍五入
                                                item.setTestResult(decimalNumber.toString());
                                                //下限
                                                BigDecimal xxValue = new BigDecimal(_itmRefer.getLowerLimit());
                                                //上限
                                                BigDecimal sxValue = new BigDecimal(_itmRefer.getUpperLimit());

                                                if (decimalNumber.compareTo(xxValue) < 0) {
                                                    item.setResultFlag("L");
                                                    isDetailRef = true;
                                                    isFactorTrue = true;
                                                }
                                                if (decimalNumber.compareTo(sxValue) > 0) {
                                                    item.setResultFlag("H");
                                                    isDetailRef = true;
                                                    isFactorTrue = true;
                                                }
                                                if (decimalNumber.compareTo(xxValue) >= 0 && decimalNumber.compareTo(sxValue) <= 0) {
                                                    item.setResultFlag("M");
                                                    isDetailRef = true;
                                                    isFactorTrue = true;
                                                }
                                            } catch (Exception e) {
                                                System.out.println("转换结果异常！");
                                            }
                                        }
                                        refStr = _itmRefer.getRange();
                                    }

                                    if (isFactorTrue) {
                                        break;
                                    }
                                    continue;
                                }

                                String sql = "select count(1) as result from dual where 1=1 ";
                                String afterReplace = new CalcItemFormulaReplcae().ReplaceCondition(_itmRefer.getFactor().toLowerCase(), mainInfo, null);
                                if (StringUtils.isEmpty(afterReplace)) {
                                    //sql += " and 1=2 ";
                                } else {
                                    //处理扫码上机视图过来的数据是汉字的情况,反向映射
                                    afterReplace = afterReplace.replaceAll("男", "1").replaceAll("女", "2").replaceAll("未知", "3");
                                    sql += " and " + afterReplace;
                                }
                                try {
                                    List<Map<String, Object>> calcResult = labRptresultMapper.getTableDataBySql(sql);
                                    // 遍历 List 中的每个 Map
                                    String ResValue = "";
                                    for (Map<String, Object> map : calcResult) {
                                        // 遍历 Map 中的键值对
                                        for (Map.Entry<String, Object> entry : map.entrySet()) {
                                            String key = entry.getKey();
                                            Object value = entry.getValue();
                                            ResValue = value.toString();
                                        }
                                    }
                                    if (!StringUtils.isEmpty(ResValue) && ResValue.equals("1")) {
                                        if (itemDict.getItemKind().equals("1")) {
                                            //检测结果
                                            if (!StringUtils.isEmpty(item.getTestResult())) {
                                                try {
                                                    BigDecimal decimalNumber = new BigDecimal(item.getTestResult());
                                                    decimalNumber = decimalNumber.setScale(_decPos, BigDecimal.ROUND_HALF_UP); // 设置小数位数并进行四舍五入
                                                    item.setTestResult(decimalNumber.toString());
                                                    //下限
                                                    BigDecimal xxValue = new BigDecimal(_itmRefer.getLowerLimit());
                                                    //上限
                                                    BigDecimal sxValue = new BigDecimal(_itmRefer.getUpperLimit());

                                                    if (decimalNumber.compareTo(xxValue) < 0) {
                                                        item.setResultFlag("L");
                                                        isDetailRef = true;
                                                        refStr = _itmRefer.getRange();
                                                        isFactorTrue = true;
                                                    }
                                                    if (decimalNumber.compareTo(sxValue) > 0) {
                                                        item.setResultFlag("H");
                                                        isDetailRef = true;
                                                        refStr = _itmRefer.getRange();
                                                        isFactorTrue = true;
                                                    }
                                                    if (decimalNumber.compareTo(xxValue) >= 0 && decimalNumber.compareTo(sxValue) <= 0) {
                                                        item.setResultFlag("M");
                                                        isDetailRef = true;
                                                        refStr = _itmRefer.getRange();
                                                        isFactorTrue = true;
                                                    }
                                                } catch (Exception e) {
                                                    System.out.println("转换结果异常！");
                                                }
                                            }
                                            item.setDisplayOrder(itemDict.getSeq().toString());
                                            item.setRef(_itmRefer.getRange());
                                            refStr = _itmRefer.getRange();
                                        }
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                                /*如果有已满足条件的 参考范围 直接跳过结算下一个 参考范围*/
                                if (isFactorTrue) {
                                    break;
                                }
                            }
                            item.setRef(StringUtils.isEmpty(refStr) ? "" : refStr);
                            item.setDisplayOrder(itemDict.getSeq().toString());

                            if (!isDetailRef && StringUtils.isEmpty(item.getResultFlag())) {
                                item.setResultFlag("M");
                            }

                            /*启用兜底的参考范围*/
                            if (_lst.isEmpty() || !isDetailRef) {
                                if (!StringUtils.isEmpty(itemDict.getLnum())) {
                                    try {
                                        //检测结果
                                        if (!StringUtils.isEmpty(item.getTestResult())) {
                                            try {
                                                BigDecimal decimalNumber = new BigDecimal(item.getTestResult());
                                                //下限
                                                BigDecimal xxValue = new BigDecimal(itemDict.getLnum());

                                                if (decimalNumber.compareTo(xxValue) < 0) {
                                                    item.setResultFlag("L");
                                                }
                                                if (!StringUtils.isEmpty(itemDict.getLflag()) && decimalNumber.compareTo(xxValue) < 0) {
                                                    item.setResultFlag(itemDict.getLflag());
                                                } else if (!StringUtils.isEmpty(itemDict.getLflag())) {
                                                    item.setResultFlag(itemDict.getLflag());
                                                } else if (decimalNumber.compareTo(xxValue) > 0) {
                                                    item.setResultFlag("M");
                                                }

                                            } catch (Exception e) {
                                                System.out.println("转换结果异常！");
                                            }
                                        }
                                        item.setDisplayOrder(itemDict.getSeq().toString());
                                        item.setRef(itemDict.getRef());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (!StringUtils.isEmpty(itemDict.getHnum())) {
                                    try {
                                        //检测结果
                                        if (!StringUtils.isEmpty(item.getTestResult())) {
                                            try {
                                                BigDecimal decimalNumber = new BigDecimal(item.getTestResult());

                                                //上限
                                                BigDecimal sxValue = new BigDecimal(itemDict.getHnum());

                                                if (decimalNumber.compareTo(sxValue) > 0) {
                                                    item.setResultFlag("H");
                                                    isDetailRef = true;
                                                }

                                                if (!StringUtils.isEmpty(itemDict.getHflag()) && decimalNumber.compareTo(sxValue) > 0) {
                                                    item.setResultFlag(itemDict.getHflag());
                                                } else if (!StringUtils.isEmpty(itemDict.getLflag()) && decimalNumber.compareTo(sxValue) >= 0) {
                                                    item.setResultFlag("P");
                                                }

                                            } catch (Exception e) {
                                                System.out.println("转换结果异常！");
                                            }
                                        }
                                        item.setDisplayOrder(itemDict.getSeq().toString());
                                        item.setRef(itemDict.getRef());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        } catch (Exception e) {
                            //todo
                        } finally {
                            countDownLatch.countDown();
                        }
                    });
                }
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                executorService.shutdown(); // 关闭线程池
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /*危急值*/
    public void CalcItemDictCriticalV1(LabMaininfo mainInfo, List<LabRptresult> rptresultlist, List<LabItemDict> itemList) {
        MPJLambdaWrapper<LabItemDictCritical> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabItemDictRefer::getDelFlag, "0");
        queryWrapper.eq(LabItemDictRefer::getIsEnable, "1");
        queryWrapper.eq(LabItemDictRefer::getRptGroup, mainInfo.getRptGroup());
        List<String> itemNos = rptresultlist.stream().map(LabRptresult::getItemNo).collect(Collectors.toList());

        if (!itemNos.isEmpty()) {
            queryWrapper.in(LabItemDictCritical::getItemNo, itemNos);
        } else {
            queryWrapper.eq(LabItemDictCritical::getOrgId, "-123!@#");
        }
        queryWrapper.orderByAsc(LabItemDictCritical::getSeq);
        List<LabItemDictCritical> LabItemDictCriticals = labItemDictCriticalMapper.selectList(queryWrapper);

        /*系统参数*/
        /*系统选项参数 wjzyssfxswbjys */
        SysParamSearchOutputDto paramResult = redisSysParamHelper.getValueByXTXXXParam(mainInfo.getOrgId(), "wjzyssfxswbjys", "1");//.getValueByKeys(param);

        try {
            for (LabRptresult item : rptresultlist) {
                if (StringUtils.isEmpty(item.getTestResult())) {
                    continue;
                }
                //报告项目
                List<LabItemDict> itmLst = itemList.stream().filter(p -> p.getItemNo().equals(item.getItemNo()) && p.getRptGroup().equals(item.getRptGroup())).collect(Collectors.toList());
                LabItemDict itemDict = null;
                if (!itmLst.isEmpty()) {
                    itemDict = itmLst.get(0);
                }
                if (BeanUtil.isEmpty(itemDict)) {
                    continue;
                }

                //检测结果如果是数值类型的则质控，文本类型的不处理
                try {
                    new BigDecimal(item.getTestResult());
                    item.setIsAlarm(0);
                    item.setAlarmText("");
                    item.setAlarmType("");
                    item.setCatchColor("");
                    item.setColorFor("0");
                } catch (Exception e) {
                    System.out.println("转换检测结果为数值失败！结果为文字类型");
                }

                List<LabItemDictCritical> _lst = LabItemDictCriticals.stream().filter(p -> p.getItemNo().equals(item.getItemNo()) && p.getRptGroup().equals(item.getRptGroup())).collect(Collectors.toList());
                for (LabItemDictCritical _itmRefer : _lst) {

                    //没有条件
                    if (StringUtils.isEmpty(_itmRefer.getFactor())) {
                        if (itemDict.getItemKind().equals("1")) {
                            if (StringUtils.isEmpty(item.getTestResult())) {
                                continue;
                            }
                            //检测结果
                            try {
                                BigDecimal decimalNumber = new BigDecimal(item.getTestResult());
                                //下限
                                BigDecimal xxValue = null;
                                if (!StringUtils.isEmpty(_itmRefer.getLowerLimit())) {
                                    xxValue = new BigDecimal(_itmRefer.getLowerLimit());
                                }
                                //上限
                                BigDecimal sxValue = null;
                                if (!StringUtils.isEmpty(_itmRefer.getUpperLimit())) {
                                    sxValue = new BigDecimal(_itmRefer.getUpperLimit());
                                }
                                if (!BeanUtil.isEmpty(xxValue) && decimalNumber.compareTo(xxValue) < 0) {
                                    item.setIsAlarm(1);
                                    item.setAlarmText(_itmRefer.getDisplayText());
                                    if (ToolsUtils.isNotEmpty(_itmRefer.getLowText())) {
                                        item.setAlarmText(_itmRefer.getLowText());
                                    }
                                    /*判断危急值显示方式*/
                                    item.setAlarmType("1");
                                    item.setCatchColor(_itmRefer.getLowCatchColor());
                                    item.setColorFor(_itmRefer.getColorFor());
                                    if (ToolsUtils.isEmpty(_itmRefer.getColorFor()) || _itmRefer.getColorFor().equals("0")) {
                                        if (ToolsUtils.isNotEmpty(paramResult.getValueStr()) && ToolsUtils.isNotEmpty(paramResult.getStatusCode()) && paramResult.getStatusCode() == 2) {
                                            if ("1".equals(paramResult.getValueStr())) {
                                                item.setColorFor(paramResult.getValueStr());
                                            }
                                        }
                                    }
                                }
                                if (!BeanUtil.isEmpty(sxValue) && decimalNumber.compareTo(sxValue) > 0) {
                                    item.setIsAlarm(1);
                                    item.setAlarmText(_itmRefer.getDisplayText());
                                    if (ToolsUtils.isNotEmpty(_itmRefer.getUpText())) {
                                        item.setAlarmText(_itmRefer.getUpText());
                                    }
                                    /*判断危急值显示方式*/
                                    item.setAlarmType("2");
                                    item.setCatchColor(_itmRefer.getCatchColor());
                                    item.setColorFor(_itmRefer.getColorFor());
                                    if (ToolsUtils.isEmpty(_itmRefer.getColorFor()) || _itmRefer.getColorFor().equals("0")) {
                                        if (ToolsUtils.isNotEmpty(paramResult.getValueStr()) && ToolsUtils.isNotEmpty(paramResult.getStatusCode()) && paramResult.getStatusCode() == 2) {
                                            if ("1".equals(paramResult.getValueStr())) {
                                                item.setColorFor(paramResult.getValueStr());
                                            }
                                        }
                                    }
                                }
                            } catch (Exception e) {
                                System.out.println("转换结果异常！");
                            }
                        }
                        continue;
                    }


                    String sql = "select count(1) as result from dual where 1=1 ";
                    String afterReplace = new CalcItemFormulaReplcae().ReplaceCondition(_itmRefer.getFactor().toLowerCase(), mainInfo, null);
                    if (StringUtils.isEmpty(afterReplace)) {
                        //sql += " and 1=2 ";
                    } else {
                        sql += " and " + afterReplace;
                    }
                    try {
                        List<Map<String, Object>> calcResult = labRptresultMapper.getTableDataBySql(sql);
                        // 遍历 List 中的每个 Map
                        String ResValue = "";
                        for (Map<String, Object> map : calcResult) {
                            // 遍历 Map 中的键值对
                            for (Map.Entry<String, Object> entry : map.entrySet()) {
                                String key = entry.getKey();
                                Object value = entry.getValue();
                                ResValue = value.toString();
                            }
                        }
                        if (!StringUtils.isEmpty(ResValue) && ResValue.equals("1")) {
                            if (itemDict.getItemKind().equals("1")) {
                                if (StringUtils.isEmpty(item.getTestResult())) {
                                    continue;
                                }
                                //检测结果
                                try {
                                    BigDecimal decimalNumber = new BigDecimal(item.getTestResult());

                                    //下限
                                    BigDecimal xxValue = null;
                                    if (!StringUtils.isEmpty(_itmRefer.getLowerLimit())) {
                                        xxValue = new BigDecimal(_itmRefer.getLowerLimit());
                                    }

                                    //上限
                                    BigDecimal sxValue = null;
                                    if (!StringUtils.isEmpty(_itmRefer.getUpperLimit())) {
                                        sxValue = new BigDecimal(_itmRefer.getUpperLimit());
                                    }
                                    if (!BeanUtil.isEmpty(xxValue) && decimalNumber.compareTo(xxValue) < 0) {
                                        item.setIsAlarm(1);
                                        item.setAlarmText(_itmRefer.getDisplayText());
                                        if (ToolsUtils.isNotEmpty(_itmRefer.getLowText())) {
                                            item.setAlarmText(_itmRefer.getLowText());
                                        }
                                        /*判断危急值显示方式*/
                                        item.setAlarmType("1");
                                        item.setCatchColor(_itmRefer.getLowCatchColor());
                                        item.setColorFor(_itmRefer.getColorFor());
                                        if (ToolsUtils.isEmpty(_itmRefer.getColorFor()) || _itmRefer.getColorFor().equals("0")) {
                                            if (ToolsUtils.isNotEmpty(paramResult.getValueStr()) && ToolsUtils.isNotEmpty(paramResult.getStatusCode()) && paramResult.getStatusCode() == 2) {
                                                if ("1".equals(paramResult.getValueStr())) {
                                                    item.setColorFor(paramResult.getValueStr());
                                                }
                                            }
                                        }
                                    }
                                    if (!BeanUtil.isEmpty(sxValue) && decimalNumber.compareTo(sxValue) > 0) {
                                        item.setIsAlarm(1);
                                        item.setAlarmText(_itmRefer.getDisplayText());
                                        if (ToolsUtils.isNotEmpty(_itmRefer.getUpText())) {
                                            item.setAlarmText(_itmRefer.getUpText());
                                        }
                                        /*判断危急值显示方式*/
                                        item.setAlarmType("2");
                                        item.setCatchColor(_itmRefer.getCatchColor());
                                        item.setColorFor(_itmRefer.getColorFor());
                                        if (ToolsUtils.isEmpty(_itmRefer.getColorFor()) || _itmRefer.getColorFor().equals("0")) {
                                            if (ToolsUtils.isNotEmpty(paramResult.getValueStr()) && ToolsUtils.isNotEmpty(paramResult.getStatusCode()) && paramResult.getStatusCode() == 2) {
                                                if ("1".equals(paramResult.getValueStr())) {
                                                    item.setColorFor(paramResult.getValueStr());
                                                }
                                            }
                                        }
                                    }
                                } catch (Exception e) {
                                    System.out.println("转换结果异常！");
                                }
                            }
                        }
                    } catch (Exception ex) {
                        //todo
                    }
                }
            }
        } catch (Exception e) {
            //TODO
        }
    }

    /*特殊规则*/
    public void CalcItemDictSpecialRulesV1(LabMaininfo mainInfo, List<LabRptresult> rptresultlist) {
        //判断报告单元结果中是否有特殊规则
        List<String> collect = rptresultlist.stream().map(LabRptresult::getItemNo).collect(Collectors.toList());
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(collect)) {
            LambdaQueryWrapper<LabItemDictSpecialRules> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabItemDictSpecialRules::getRptGroup, mainInfo.getRptGroup());
            lambdaQueryWrapper.eq(LabItemDictSpecialRules::getIsEnable, 1);
            lambdaQueryWrapper.eq(LabItemDictSpecialRules::getDelFlag, 0);
            lambdaQueryWrapper.in(LabItemDictSpecialRules::getItemNo, collect);
            List<LabItemDictSpecialRules> labItemDictSpecialRules = labItemDictSpecialRulesMapper.selectList(lambdaQueryWrapper);
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(labItemDictSpecialRules)) {
                return;
            }
            //循环规则列表，判断结果值是否满足，满足则更新结果标识
            labItemDictSpecialRules.forEach(a -> {
                try {
                    Optional<LabRptresult> first = rptresultlist.stream().filter(p -> p.getItemNo().equals(a.getItemNo())).findFirst();
                    if (first.isPresent()) {
                        LabRptresult labRptresult = first.get();
                        //套入sql判断是否满足公式结果
                        if (StrUtil.isNotEmpty(labRptresult.getTestResult())) {
                            String sql = "select count(1) as result from dual where 1=1 ";
                            String afterReplace = new CalcItemFormulaReplcae().ReplaceCondition(a.getFactor().toLowerCase(), mainInfo, labRptresult);
                            if (StrUtil.isNotEmpty(afterReplace)) {
                                sql += " and " + afterReplace;
                            }
                            try {
                                List<Map<String, Object>> calcResult = labRptresultMapper.getTableDataBySql(sql);
                                System.out.println(calcResult);
                                if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(calcResult) && "1".equals(calcResult.get(0).get("result") + "")) {
                                    labRptresult.setResultFlag(a.getExceptionSymbol());
                                    labRptresult.setExceptionSymbol(a.getResultFlag());
                                    labRptresultMapper.updateById(labRptresult);
                                }
                            } catch (Exception e) {
                                System.out.println("处理特殊规则结果异常");
                            }
                        }
                    }
                } catch (Exception e) {
                    System.out.println("处理特殊规则结果异常");
                }
            });
        }
    }
}
